<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<div id="userGroup-select-dialog" style="display:none;padding:10px;">
  <h4>选择</h4>
  <div style="width:90%;height:260px">
    <ul id="userGroup-select-tree" class="ztree"></ul>
  </div>
  <hr>
  <p class="text-center">
      <button id="userGroup-select-confirm" type="button" class="btn btn-info"><s:message code="btn.confirm" /></button>
      <button id="userGroup-select-cancel" type="button" class="btn"><s:message code="btn.cancel" /></button> 
  </p>
</div>
<script type="text/javascript">
var __userGroup_select_dialog = null;
var __selectUserGroupIds = null;

function openSelectUserGroupTreeDialog()
{
  __userGroup_select_dialog = layer.open({
    type: 1,
    title: false,
    area: ['400px', '420px'],
    skin: 'layui-layer-rim', //加上边框
    shadeClose: true,
    fix: false,
    content: $('#userGroup-select-dialog')
  });
}

function selectUserGroupTreeOnClick(event, treeId, treeNode) {
    var id = treeNode.id;
    __selectUserGroupIds = id;
};
  
function selectUserGroupTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    var node = treeObj.getNodeByParam("id", 0, null);
    treeObj.selectNode(node);
};            
  
  $(document).ready(function(){ 
    var setting = {
        data: {          
          simpleData: {
            enable: true
            ,idKey: "id"
            ,pIdKey: "parentId"
          }
        }
        ,callback: {
          onClick: selectUserGroupTreeOnClick
          ,onAsyncSuccess: selectUserGroupTreeOnAsyncSuccess
        }
        ,async: {
          enable: true,//启用异步加载
          url:"${contextPath}/system/userGroup/getTree.json", //异步请求地址
          autoParam:["id"], //需要传递的参数,为你在ztree中定义的参数名称
          dataFilter: function(treeId, parentNode, childNodes){
            for(var i=0;i<childNodes.length;i++)
            {
              var child = childNodes[i];
              if(child.type == 1)
                child.iconSkin = "building";
              else if(child.type==2)
                child.iconSkin = "department";
              else if(child.type==3)
                child.iconSkin = "group";
            }
            return childNodes;
          }
        }
    };
    
    var zNodes =[
      { id:0, pId:-1, name:"<s:message code='userGroup'/>", open:true, isParent:true, iconSkin:"home"}
    ]; 
    
    var treeObj = $.fn.zTree.init($("#userGroup-select-tree"), setting, zNodes);   
    var node = treeObj.getNodeByParam("id", 0, null);
    treeObj.reAsyncChildNodes(node, "refresh");
    
    $("#userGroup-select-confirm").click(function(){
      onSelectUserGroupTreeConfirm(__selectUserGroupIds);	//该方法由子页面实现
    });
    
    $("#userGroup-select-cancel").click(function(){
      layer.close(__userGroup_select_dialog);
    });
    
    
  });
</script>  